package cn.hncu.booksManagementSystem.user.ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import cn.hncu.booksManagementSystem.common.enums.UserTypeEnum;
import cn.hncu.booksManagementSystem.user.business.ebi.UserEbi;
import cn.hncu.booksManagementSystem.user.business.factory.UserEbiFactory;
import cn.hncu.booksManagementSystem.user.vo.UserModel;

public class UpdatePanel extends JPanel {

	private static final long serialVersionUID = 1L;
	
	private JTextField tfdId;
	private JTextField tfdName;
	private JPasswordField tfdPwd;
	
	private JFrame mainFrame;
	private UserModel oldUser;
	
	//注入ebi
	private UserEbi ebi = UserEbiFactory.getInstanceOfUserEbi();
	/**
	 * Create the panel.
	 */
	public UpdatePanel(JFrame mainFrame,UserModel oldUser) {
		this.mainFrame = mainFrame;
		this.oldUser = oldUser;
		initContentPane();
	}
	
	public void initContentPane() {
		setLayout(null);
		
		JLabel label = new JLabel("修改用户");
		label.setForeground(Color.RED);
		label.setFont(new Font("微软雅黑", Font.BOLD, 36));
		label.setHorizontalAlignment(SwingConstants.CENTER);
		label.setBounds(285, 33, 210, 49);
		add(label);
		
		JLabel lblid = new JLabel("ID：");
		lblid.setFont(new Font("微软雅黑", Font.BOLD, 24));
		lblid.setHorizontalAlignment(SwingConstants.CENTER);
		lblid.setBounds(82, 141, 121, 34);
		add(lblid);
		
		tfdId = new JTextField(oldUser.getId());
		tfdId.setEnabled(false);
		tfdId.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdId.setBounds(210, 142, 171, 33);
		add(tfdId);
		tfdId.setColumns(10);
		
		JLabel label_1 = new JLabel("名称：");
		label_1.setHorizontalAlignment(SwingConstants.CENTER);
		label_1.setFont(new Font("微软雅黑", Font.BOLD, 24));
		label_1.setBounds(82, 242, 105, 34);
		add(label_1);
		
		tfdName = new JTextField(oldUser.getName());
		tfdName.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdName.setColumns(10);
		tfdName.setBounds(197, 242, 171, 33);
		add(tfdName);
		
		JLabel label_2 = new JLabel("密码：");
		label_2.setHorizontalAlignment(SwingConstants.CENTER);
		label_2.setFont(new Font("微软雅黑", Font.BOLD, 24));
		label_2.setBounds(415, 246, 96, 34);
		add(label_2);
		
		tfdPwd = new JPasswordField();
		tfdPwd.setFont(new Font("微软雅黑", Font.PLAIN, 16));
		tfdPwd.setBounds(505, 242, 171, 34);
		add(tfdPwd);
		
		JComboBox<String> cbbxType = new JComboBox<String>();
		cbbxType.setFont(new Font("微软雅黑", Font.PLAIN, 24));
		for (UserTypeEnum ute : UserTypeEnum.values()) {
			cbbxType.addItem(ute.getName());
		}
		cbbxType.setBounds(464, 138, 184, 37);
		add(cbbxType);
		cbbxType.setSelectedItem(UserTypeEnum.getNameByType(oldUser.getType()));
		
		JButton btnUpdate = new JButton("修改");
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				//1收集参数 id,name，pwd，type
				String id = tfdId.getText();
				String name = tfdName.getText();
				@SuppressWarnings("deprecation")
				String pwd = tfdPwd.getText();
				int type = UserTypeEnum.getTypeByName(cbbxType.getSelectedItem().toString());
				
				if (name==null || name.trim().length()==0) {
					JOptionPane.showMessageDialog(null, "用户名不能为空！");
					return;
				}
				boolean isLegal = name.matches("^\\w{3,10}$");
				if (!isLegal) {
					JOptionPane.showMessageDialog(null, "用户名格式错误，只能由字母、数字和_组成，并且长度在3-10之间");
					return;
				}
				
				//如果没有输入密码，则保持原有的密码
				if (pwd==null|| pwd.trim().length()==0) {
					pwd=oldUser.getPwd();
				}
				isLegal = pwd.matches("^\\w{3,10}$");
				if (!isLegal) {
					JOptionPane.showMessageDialog(null, "密码格式错误，只能由字母、数字和_组成，并且长度在3-10之间");
					return;
				}
				
				//2组织参数
				UserModel newUser = new UserModel();
				newUser.setId(id);
				newUser.setName(name);
				newUser.setPwd(pwd);
				newUser.setType(type);
				//3 调用逻辑层
				boolean boo = ebi.updateUserInfo(newUser);
				//4 导向结果界面
				if (boo) {
					mainFrame.setContentPane(new ListPanel(mainFrame));
					mainFrame.validate();
				}else {
					JOptionPane.showMessageDialog(null, "系统繁忙！");
				}
			}
		});
		btnUpdate.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnUpdate.setBounds(209, 333, 131, 49);
		add(btnUpdate);
		
		JButton btnBack = new JButton("返回");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				mainFrame.setContentPane(new ListPanel(mainFrame));
				mainFrame.validate();
			}
		});
		btnBack.setFont(new Font("微软雅黑", Font.BOLD, 24));
		btnBack.setBounds(432, 333, 131, 49);
		add(btnBack);
	}
}
